// Package controllers
// @Title  default.go
// @Description  默认控制器
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)
package controllers

import (
	"beego-admin/models"
	"beego-admin/service"
)

type ArticleController struct {
	BaseController
}

func (this *ArticleController) Index() {
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("page_size",6)
	//查询首页推荐列表
	queryParams := models.ArticleQueryParams{
		PageSize: pageSize,
		Page:  page,
		CatId:this.catId,
	}

	articleList,count,_ := models.GetArticleList(queryParams)
	articleData := service.ArticleListFormat(articleList)

	catName := ""
	if this.catId > 0{
		articleCatInfo,_ := models.GetArticleCatInfoById(this.catId)
		catName = articleCatInfo.CatName
	}

	this.Data["article_list"] = articleData
	this.Data["cat_name"] = catName
	this.Data["count"] = count
	this.Data["page"] = page
	this.Data["page_size"] = pageSize
	this.Data["page_title"] = catName
	this.TplName = "index/article.tpl"
}

func (this *ArticleController) ArticleDetail() {
	articleId,_ := this.GetInt("article_id",1)

	articleInfo,_ := models.GetArticleInfoById(articleId)

	authorName := ""
	if articleInfo.Author > 0{
		adminUserInfo,_ := models.GetAdminUserInfoById(articleInfo.Author)
		authorName = adminUserInfo.AdminUser
	}

	addTime := articleInfo.AddTime.Format("2006-01-02 15:04:05")

	catName := ""
	if articleInfo.CatId > 0{
		articleCatInfo,_ := models.GetArticleCatInfoById(articleInfo.CatId)
		catName = articleCatInfo.CatName
	}

	this.Data["cat_name"] = catName
	this.Data["author_name"] = authorName
	this.Data["add_time"] = addTime
	this.Data["title"] = articleInfo.Title
	this.Data["content"] = articleInfo.Content
	this.Data["page_title"] = articleInfo.Title
	this.TplName = "index/article_detail.tpl"
}
